import Crypt from "../../../../utils/Crypt";
import StoreModel from "../../../../models/Store";
import OrderModel from "../../../../models/Order";

const model = new StoreModel()
const address = new OrderModel()

Page({
  data: {
    total: 0,
    addressList: [],
    remoteCity: false,
    // 提货数量
    num: 1,
    products: []
  },
  onLoad: function (options) {
    // this.getAddressList()
  },

  changeNum(e) {
    const item = e.detail.value
    this.setData({ num: +item })
    // console.log(this.data.num, item)
  },

  onShow() {
    this.getAddressList()
    this.getCounter()
  },

  // 数量
  getCounter() {
    model.getCount().then(response => {
      let { data } = response
      data = Crypt.parse(data)
      // data = data.model
      // console.log(data.products)
      const products = data.products
      // 選擇的數量
      products.forEach(x => {
        x.num = 0
      })
      this.setData({ total: data.model.num, products: products })
    })
  },

  // 地址列表
  getAddressList() {
    address.getAddress().then(response => {
      let { data } = response
      data = Crypt.parse(data)
      data.forEach((v, k) => {
        if (k === 0) {
          v.selected = true
          this.isRemoteCity(v.province_id)
        } else {
          v.selected = false
        }
      })
      this.setData({
        addressList: data
      })
    })
  },

  // 远程地址
  isRemoteCity(province_id) {
    address.isRemoteCity({province_id}).then(response => {
      let { data } = response
      data = Crypt.parse(data)
      this.setData({
        remoteCity: data.result
      })
    })
  },

  // 选择地址
  selectedAddress(e) {
    const item = e.detail.item
    const list = [...this.properties.addressList]
    list.forEach(x => {
      if (x.id === item.id) {
        x.selected = true
        this.isRemoteCity(x.province_id)
      } else {
        x.selected = false
      }
    })
    this.setData({ addressList: list })
  },

  // 选择商品
  selectedProduct(e) {
    // const item = e.detail.item
    // console.log(item)
    // const list = [...this.properties.addressList]
    // list.forEach(x => {
    //   if (x.id === item.id) {
    //     x.selected = true
    //     this.isRemoteCity(x.province_id)
    //   } else {
    //     x.selected = false
    //   }
    // })
    // this.setData({ addressList: list })
  },

  // 選擇商品數量
  changeProductNum(e) {
    // console.log(e)
    const num =  e.detail.num
    const item = e.detail.item
    // console.log(num, item)
    const id = item.id
    const products = this.data.products
    products.forEach(x => {
      if (x.id === id) {
        x.num = +num
      }
    })
    this.setData({
      products
    })
  },

  // 提交
  go() {

    const addressList = this.data.addressList
    if (!addressList || addressList.length === 0) {
      const type = '-2'
      return wx.navigateTo({
        url: '/sub_service/pages/address-edit/index?type=' + type
      })
    }

    const num = this.data.num
    const address_id = addressList.filter(x => x.selected)[0].id

    // 老的
    if (num > this.data.total || num <= 0) {
      // return wx.showToast({ title: '数量错误', icon: "none", mask: true })
    }

    if (!address_id) {
      return wx.showToast({ title: '地址错误', icon: "none", mask: true })
    }

    const selectNum = this._getTotal()
    if (selectNum <= 0 || selectNum > this.data.total) {
      return wx.showToast({ title: '选择数量错误', icon: "none", mask: true })
    }

    model.extract({
      num,
      address_id,
      products: this.data.products
    }).then(() => {
      wx.showToast({ title: '操作成功' })
      setTimeout(() => {
        wx.navigateTo({
          url: '/sub_service/pages/store/list/index?type=3'
        })
      }, 500)
    })
  },

  // 获取数量
  _getTotal() {
    let sum = 0;
    this.data.products.forEach(x => {
      sum += x.num
    })
    return sum
  },

});
